package com.uton.rfidcontroller.view.adapter.task;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.databinding.library.baseAdapters.BR;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.databinding.ItemHasDoTaskWrittenListBinding;
import com.uton.rfidcontroller.model.CarDealerListWriteInModel;
import com.uton.rfidcontroller.view.activity.write.WriteActivity;
import com.uton.rfidcontroller.view.base.BaseRecyclerViewWrittenFragment;
import com.uton.rfidcontroller.view.fragment.HasWrittenFragment;

import java.util.List;


public class TaskListHasDoWrittenAdapter extends BaseRecyclerAdapter<TaskListHasDoWrittenAdapter.SimpleAdapterViewHolder> {
    private List<CarDealerListWriteInModel> data;
    private BaseRecyclerViewWrittenFragment mContext;

    public TaskListHasDoWrittenAdapter(BaseRecyclerViewWrittenFragment context) {
        mContext = context;
    }

    @Override
    public int getAdapterItemViewType(int position) {
        return 0;
    }

    @Override
    public int getAdapterItemCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public SimpleAdapterViewHolder getViewHolder(View view) {
        return new SimpleAdapterViewHolder(view, false);
    }

    public void setData(List<CarDealerListWriteInModel> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public SimpleAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        ItemHasDoTaskWrittenListBinding mItemBinding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_has_do_task_written_list, parent, false);

        SimpleAdapterViewHolder mHolder = new SimpleAdapterViewHolder(mItemBinding.getRoot(), true);
        mHolder.setBinding(mItemBinding);//把mItemBinding设置给ViewHolder

        return mHolder;
    }

    @Override
    public void onBindViewHolder(final TaskListHasDoWrittenAdapter.SimpleAdapterViewHolder holder, int position, boolean isItem) {
        //        通过holder.getBinding()得到Binding Class
        holder.getBinding().setVariable(BR.model, data.get(holder.getAdapterPosition()));

        holder.getBinding().rlCarListItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((HasWrittenFragment) mContext).startActivityForResult(new IntentBuilder().from(mContext.getActivity(), WriteActivity.class).
                        setStringParam(GlobalConstants.EXTRA_STRING_PARAM1, data.get(holder.getAdapterPosition()).getCarDealerId()).
                        setStringParam(GlobalConstants.EXTRA_STRING_PARAM2, data.get(holder.getAdapterPosition()).getWriteInNum()).
                        getIntent(), GlobalConstants.REQUEST_WANT_REFRESH);
            }
        });

        holder.getBinding().executePendingBindings();//立即更新UI
    }


    public void insert(CarDealerListWriteInModel s, int position) {
        insert(data, s, position);
    }

    public void clear() {
        if (data != null) {
            clear(data);
        }
    }

    class SimpleAdapterViewHolder extends RecyclerView.ViewHolder {
        private ItemHasDoTaskWrittenListBinding binding;

        public ItemHasDoTaskWrittenListBinding getBinding() {
            return binding;
        }

        public void setBinding(ItemHasDoTaskWrittenListBinding binding) {
            this.binding = binding;
        }

        SimpleAdapterViewHolder(View itemView, boolean isItem) {
            super(itemView);
        }


    }

}